package ywpt.service.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ClientInfo;
import ywpt.model.ClientKeshi;
import ywpt.web.common.PublicFunction;

public class ClientKeshiServiceImpl  extends HibernateEntityDao<ClientKeshi> implements ClientKeshiService{
	public ClientKeshiServiceImpl(){
		
	}

	@Override
	public String CreateCode(Long clientId) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientKeshi.class, "keshi");
		query.add(Restrictions.eq("client", this.get(ClientInfo.class, clientId)));
		String keshiCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<ClientKeshi> clientKeshis = this.getClientKeshis(query);
		if ( clientKeshis.iterator().hasNext() )	
		{
			code = clientKeshis.iterator().next().getCode();
			maxId = new Integer(code);
		}
		keshiCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return keshiCode;
	}

	@Override
	public ClientKeshi getClientKeshiById(Long id) {
		return this.get(id);
	}

	@Override
	public void removeClientKeshi(Long id) {
		super.removeById(id);
	}

	@Override
	public void saveClientKeshi(ClientKeshi clientKeshi, Long clientId) {
		if(clientId != null){
			clientKeshi.setClient(this.get(ClientInfo.class, clientId));
		}
		super.save(clientKeshi);
	}

	@Override
	public void updateClientKeshi(ClientKeshi clientKeshi, Long clientId) {
		if(clientId != null){
			clientKeshi.setClient(this.get(ClientInfo.class, clientId));
		}
		super.update(clientKeshi);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientKeshi> getClientKeshis(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientKeshi> getClientKeshiByKeshiName(String keshiName,
			Long clientId, int start, int size) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientKeshi.class, "keshi");
		query.add(Restrictions.eq("client", this.get(ClientInfo.class, clientId)));
		if ( keshiName != null && !keshiName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("name", "%" + keshiName + "%"),  
							          Restrictions.like("code", "%" + keshiName + "%")));
		}
		query.addOrder(Order.asc("code"));
		List<ClientKeshi> clientKeshiList = new ArrayList<ClientKeshi>();
		List<ClientKeshi> clientKeshiLists = new ArrayList<ClientKeshi>();
		ClientKeshi keshi =new ClientKeshi();
		
		clientKeshiList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<ClientKeshi> itr = clientKeshiList.iterator(); itr.hasNext(); i++ ) 
		{
			keshi = itr.next();
			String ss = PublicFunction.format(keshi.getCode() + "", 6, "  ", 1)
			          + keshi.getName(); 
			
			keshi.setName(ss);
			clientKeshiLists.add(keshi);
		}
		return clientKeshiLists;
	}

	@Override
	public int getClientKeshiCountByName(String keshiName, Long clientId) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientKeshi.class, "keshi");
		query.add(Restrictions.eq("client", this.get(ClientInfo.class, clientId)));
		if ( keshiName != null && !keshiName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("name", "%" + keshiName + "%" ),  
							          Restrictions.like("code", "%" + keshiName + "%" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	@Override
	public String getKeshiDoorCode(Long id) {
		return this.get(id).getDoorCode();
	}
}
